package com.lee.message.core;

import com.lee.message.core.aspectj.rocketmq.RocketMqIdempotentAspect;
import com.lee.message.core.converter.MessageConverter;
import com.lee.message.core.converter.rocketmq.RocketMQMessageConverter;
import com.lee.message.core.config.MqIdempotentProperties;
import com.lee.message.core.config.cache.Cache;
import com.lee.message.core.config.cache.redis.RedisCache;
import com.lee.message.core.config.lock.Lock;
import com.lee.message.core.config.lock.redis.RedisLock;
import com.lee.message.core.transaction.CustomRocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 启动类
 */
@Configuration
@EnableConfigurationProperties(MqIdempotentProperties.class)
@ConditionalOnProperty(prefix = MqIdempotentProperties.PREFIX, name = "enabled", havingValue = "true")
public class MqIdempotentAutoConfiguration {

    @Configuration
    @ConditionalOnClass(StringRedisTemplate.class)
    @ConditionalOnProperty(prefix = MqIdempotentProperties.PREFIX, name = "repository", havingValue = "REDIS", matchIfMissing = true)
    public static class CustomRedisConfiguration {

        /**
         * 分布式锁
         * @param connectionFactory
         * @return
         */
        @Bean
        public Lock customRedisLock(final RedisConnectionFactory connectionFactory) {
            StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);
            return new RedisLock(redisTemplate);
        }

        /**
         * 缓存
         * @param connectionFactory
         * @return
         */
        @Bean
        public Cache customRedisCache(final RedisConnectionFactory connectionFactory) {
            StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);
            return new RedisCache(redisTemplate);
        }

    }

    /**
     * rocketMQ配置，转换器和对应aop拦截器
     */
    @Configuration
    @ConditionalOnProperty(prefix = MqIdempotentProperties.PREFIX, name = "component", havingValue = "ROCKETMQ", matchIfMissing = true)
    public static class CustomRocketMqConfiguration {

        @Bean
        public RocketMqIdempotentAspect rocketMqIdempotentAspect(){
            return new RocketMqIdempotentAspect();
        }

        /**
         * 没有监听则创建
         * @return
         */
        @Bean
        @ConditionalOnMissingBean(RocketMQLocalTransactionListener.class)
        public RocketMQLocalTransactionListener customRocketMQLocalTransactionListener(){
            return new CustomRocketMQLocalTransactionListener();
        }

        @Bean
        public MessageConverter messageConverter(){
            return new RocketMQMessageConverter();
        }

    }

}
